package com.mars.component.scheduler.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mars.component.scheduler.entity.SchedulerTask;
import com.mars.component.scheduler.service.SchedulerTaskService;

@Controller
@RequestMapping("/scheduler")
public class SchedulerController {
	
	@Autowired
	private SchedulerTaskService schedulerTaskService;
	
	@ResponseBody
	@RequestMapping(value="email")
	public void email() {
		try {
			SchedulerTask task = new SchedulerTask() ;
			task.setJobGroup("Email");
			task.setBeanName("emailTask");
			task.setMethodName("execute");
			task.setCronExpression("0/3 * * * * ?");
			this.schedulerTaskService.createScheduleJob(task);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@ResponseBody
	@RequestMapping(value="backup")
	public void backup() {
		try {
			SchedulerTask task = new SchedulerTask() ;
			//task.setJobGroup("Backup");
			task.setBeanName("backupTask");
			task.setMethodName("execute");
			task.setCronExpression("0/3 * * * * ?");
			this.schedulerTaskService.createScheduleJob(task);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 重置设置任务触发时间
	 */
	@ResponseBody
	@RequestMapping(value="resetCron")
	public void resetCron(Long jobId, String cron) {
		try {
			SchedulerTask task = new SchedulerTask() ;
			task.setJobId(jobId);
			task.setStatus(1);
			task.setJobGroup("Email");
			task.setBeanName("emailTask");
			task.setMethodName("execute");
			task.setCronExpression(cron);
			this.schedulerTaskService.rescheduleJob(task);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 暂停任务
	 */
	@ResponseBody
	@RequestMapping(value="pause")
	public void pause(Long jobId) {
		try {
			this.schedulerTaskService.pause(new Long[]{jobId}, new String[]{"Email"});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 重新启动任务
	 */
	@ResponseBody
	@RequestMapping(value="resume")
	public void resume(Long jobId) {
		try {
			this.schedulerTaskService.resume(new Long[]{jobId}, new String[]{"Email"});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
